package com.segi.view.refresh;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.segi.view.R;

/**
 * 这个类封装了下拉刷新的布局
 * 
 * @author liangzx
 * @version [segi, 2013-12-23] 
 */
public class FooterLoadingLayoutForUhomeNew extends LoadingLayout {
    /**进度条*/
    private ImageView mProgressBar;
    /** 显示的文本 */
    private TextView mHintView;
    AnimationDrawable mProgressAnim;
    /**
     * 构造方法
     *
     * @param context context
     */
    public FooterLoadingLayoutForUhomeNew(Context context) {
        super(context);
        init(context);
    }

    /**
     * 构造方法
     *
     * @param context context
     * @param attrs attrs
     */
    public FooterLoadingLayoutForUhomeNew(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    /**
     * 初始化
     * 
     * @param context context
     */
    private void init(Context context) {
        mProgressBar = (ImageView) findViewById(R.id.pull_to_load_footer_progressbar);
        mProgressAnim = (AnimationDrawable) mProgressBar.getDrawable();
        mHintView = (TextView) findViewById(R.id.pull_to_load_footer_hint_textview);
        
        setState(State.RESET);
    }
    
    @Override
    protected View createLoadingView(Context context, AttributeSet attrs) {
        View container = LayoutInflater.from(context).inflate(R.layout.pull_updown_footer_for_uhome_new, null);
        return container;
    }

    @Override
    public void setLastUpdatedLabel(CharSequence label) {
    }

    @Override
    public int getContentSize() {
        View view = findViewById(R.id.pull_to_load_footer_content);
        if (null != view) {
            return view.getHeight();
        }
        
        return (int) (getResources().getDisplayMetrics().density * 40);
    }
    
    @Override
    protected void onStateChanged(State curState, State oldState) {
        mProgressBar.setVisibility(View.GONE);
        mHintView.setVisibility(View.INVISIBLE);
        
        super.onStateChanged(curState, oldState);
    }
    
    @Override
    protected void onReset() {
        AnimationDrawable drawable = (AnimationDrawable) mProgressBar.getDrawable();
        if (drawable.isRunning()){
            drawable.stop();
        }
        mHintView.setText(R.string.pull_to_refresh_header_hint_loading);
    }

    @Override
    protected void onPullToRefresh() {
        mHintView.setVisibility(View.VISIBLE);
        mHintView.setText(R.string.pull_to_refresh_header_hint_normal2);
    }

    @Override
    protected void onReleaseToRefresh() {
        mHintView.setVisibility(View.VISIBLE);
        mHintView.setText(R.string.pull_to_refresh_header_hint_ready);
    }

    @Override
    protected void onRefreshing() {
        mProgressBar.setVisibility(View.VISIBLE);
        AnimationDrawable drawable = (AnimationDrawable) mProgressBar.getDrawable();
        if (drawable.isRunning()){
            drawable.stop();
        }
        drawable.start();
        mHintView.setVisibility(View.VISIBLE);
        mHintView.setText(R.string.pull_to_refresh_header_hint_loading);
    }
    
    @Override
    protected void onNoMoreData() {
        mHintView.setVisibility(View.VISIBLE);
        mHintView.setText(R.string.pushmsg_center_no_more_msg);
    }


//    /**
//     * 开始动画
//     */
//    private void startAnim() {
//
//        findViewById(R.id.pull_to_load_footer_image).setVisibility(View.GONE);
//        mProgressBar.setVisibility(View.VISIBLE);
//        if (!mProgressAnim.isRunning()) {
//            mProgressAnim.start();
//        }
//    }
//
//    /**
//     * 暂停动画
//     */
//    private void stopAnim() {
//        findViewById(R.id.pull_to_load_footer_image).setVisibility(View.VISIBLE);
//        mProgressBar.setVisibility(View.GONE);
//        if (mProgressAnim.isRunning()) {
//            mProgressAnim.stop();
//        }
//    }
}
